import { Component, OnInit } from '@angular/core';
import { FetchService } from 'src/app/core/net/fetch.service';
import { NzMessageService } from 'ng-zorro-antd';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

export interface TablePaginationModel {
    pages: number;
    total: number;
    pageNum: number;
}

export interface RoleData {
    id: string;
    name: string;
}

export interface UserInfoData {
    id: string;
    name: string;
    account: string;
    createBy: string;
    createDate: number;
    duty: string;
    email: string;
    message: string;
    orgId: string;
    orgName: string;
    password: string;
    phone: string;
    role: string;
    roleName: string;
    updateBy: string;
    updateDate: number;
    sectionId?: string;
    [key: string]: any;
}

export interface UserTableData extends TablePaginationModel {
    list: Array<UserInfoData>;
}

@Component({
    selector: 'app-user-manage',
    templateUrl: './user-manage.component.html',
    styleUrls: ['./user-manage.component.scss']
})
export class UserManageComponent implements OnInit {
    roleData: Array<RoleData> = [];
    searchName: string = '';
    tableData: Array<UserInfoData> = [];

    pageNum = 1;
    pageSize = 10;
    total: number;

    modalTitle: string = '';
    modalShow: boolean = false;
    nzBodyStyle: any = {
        padding: '16px 20px 0'
    };

    form: FormGroup;
    manageList: any[] = [];

    sectionMap = {
        '1': '-',
        '168': '第一管理所',
        '169': '第二管理所'
    };
    sectionList = [
        {
            label: '-',
            value: '1'
        },
        {
            label: '第一管理所',
            value: '168'
        },
        {
            label: '第二管理所',
            value: '169'
        },
    ];
    constructor(
        private fetchService: FetchService,
        private message: NzMessageService,
        private fb: FormBuilder
    ) {
    }

    ngOnInit() {
        this.getRoleList();
        this.getManageList();
        this.queryInfo();

        this.form = this.fb.group({
            id: [''],
            account: ['', Validators.required],
            password: ['', Validators.required],
            name: ['', Validators.required],
            roleId: ['', Validators.required],
            manageId: ['', Validators.required],
            sectionId: [''],
            mobile: [''],
            email: [''],
            remarks: ['']
        });
    }

    reset(): void {
        this.searchName = '';
        this.queryInfo();
    }

    queryInfo(): void {
        this.fetchService
            .post<any>('sys/sysUser/list', {
                name: this.searchName,
                // roleIds: this.searchRole,
                pageNum: this.pageNum,
                pageSize: this.pageSize
            })
            .subscribe(res => {
                this.total = res.total;
                this.tableData = res.list;
            });
    }

    getRoleList() {
        this.fetchService.post<any>('sys/sysRole/list', {}).subscribe(res => {
            this.roleData = res.list;
        });
    }

    getManageList() {
        this.fetchService
            .post<any>('wrs/wrsBmanage/list', {
                type: '0,1'
            })
            .subscribe(res => {
                this.manageList = res.list;
            });
    }

    openEditModal(item?): void {
        this.modalTitle = item ? `编辑${item.name}` : '新增用户';
        if (item && item.roleIds) {
            item.roleId = item.roleIds[0];
        }
        this.form.patchValue(item || {});
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.post('sys/sysUser/del', { id }).subscribe(resp => {
            this.message.info('删除成功');
            this.queryInfo();
        });
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.form.value;
        if (!data.sectionId) {
            data.sectionId = '1';
        }
        data.roleIds = [data.roleId];
        if (data.id) {
            this.fetchService.post('sys/sysUser/edit', data).subscribe(res => {
                this.modalShow = false;
                this.queryInfo();
            });
        } else {
            this.fetchService.post('sys/sysUser/add', data).subscribe(res => {
                this.modalShow = false;
                this.queryInfo();
            });
        }
    }

    onCancel() {
        this.modalShow = false;
    }
}
